var app = getApp()
var fun = require('../fun.js');
Page({
   data: { nav:{top:app.globalData.nav_top,title:'图库',home:'',back:'hide',safe_top:app.globalData.safe_top,index:'my',os:app.globalData.os,safe_bottom:app.globalData.safe_bottom},page:1,can_load:1,can_upload:1
   },

   onLoad(op) {
var msg=op.msg;if(msg){app.err(msg);}
var img=op.img;
var max=op.max;if(!max){max=1;}
var c=op.c;if(!c){c='';}
this.data.c=c;

this.setData({max:max})
if(img){
   var x=img.split('||');
   var is_select={}
   for(var i in x){
      if(x[i]){
      var z=x[i].split('|');
      is_select['item_'+z[0]]={id:z[0]}
   }
   }
   this.format_is_select(is_select);
}


   },

   onReady() {
this.load()
   },

   onShow() {
if(app.globalData.flash==1){this.load();app.globalData.flash=0;}
   },

   load() {
   var page=this.data.page;if(!page){page=1;}
   if(this.data.can_load!=1){return;}
   this.data.can_load=0;
fun.get({ac:'get_img',c:this.data.c?this.data.c:'',page:page}).then(res=>{
if(page==1){
   var no_data=0;
   if(!res.list){no_data=1;}
   this.setData({list:res.list?res.list:'',no_data:no_data,can_load:1,set:res.set})
}else{
if(res.list){var list=this.data.list;
for(var i in res.list){
   list[i]=res.list[i];
}
this.setData({list:list,can_load:1});
}
}
})
   },
   select_img(e){//上传
      var that=this;
      wx.chooseMedia({
         count: 9,
         mediaType: ['image'],
         sourceType: ['album', 'camera'],
         maxDuration: 30,//时长
         camera: 'back',
         sizeType:['original'],//是否压缩
         success(res) {console.log(res.tempFiles)
            if(res.tempFiles){
               var t=(new Date()).getTime();
            var temp={}
      for(var i in res.tempFiles){
         t++;
         temp['item_'+t]=res.tempFiles[i];
         temp['item_'+t]['id']=t;
         temp['item_'+t]['is_ok']=0;
         temp['item_'+t]['c']=that.data.c;
      }
  console.log(temp)
      that.setData({upload_file:temp,no_data:0});
      that.upload_file();
      wx.showToast({
         title: '上传中，请稍后...',
         icon: 'loading',
         duration: 2000
       });
            }
         }
       })
   },
 
   upload_file(id) {
     var upload_file=this.data.upload_file;
     if(!upload_file){return;}
     if(id){
        delete(upload_file['item_'+id]);
        this.setData({upload_file:upload_file})
     }
     var that=this;
var upload_file_num=0;
if(upload_file){
for(var i in upload_file){
if(upload_file[i].is_ok==0 && this.data.can_upload==1){upload_file_num++;
this.data.can_upload=0;
fun.upload(upload_file[i]).then(res=>{
if(res.err=='ok'){
console.log(res,upload_file['item_'+res.id],res.id);
upload_file['item_'+res.id]['is_ok']=1;
var list=that.data.list;
if(!list){list={}}
var temp={}

temp['item_'+res.img_id]={img:res.img,img_240:res.img_240,img_480:res.img_480,img_750:res.img_750,id:res.img_id}
for(var i in list){
temp[i]=list[i];
}
list=temp;
temp=''
that.setData({upload_file:upload_file,list:list});
this.data.can_upload=1;

setTimeout(function(){that.upload_file(res.id);},200);

}

})

}
}}

if(upload_file_num==0){//没有上传的数据了
this.setData({upload_file:''})
}
   },
   select_it(e){
var is_select=this.data.is_select;
if(!is_select){is_select={}}
var list=this.data.list;
var index=e.currentTarget.dataset.index;
console.log(list[index])
if(is_select['item_'+list[index].id]){
delete(is_select['item_'+list[index].id]);
}else{
is_select['item_'+list[index].id]=list[index];
}
console.log(is_select)
this.format_is_select(is_select);

   },
   format_is_select(is_select){
     var  num=0;var max=parseInt(this.data.max);if(!max){max=1}

   if(is_select){
for(var i in is_select){
   num++;
   is_select[i].num=num;
   if(num>max){
      delete(is_select[i]);
      if(max>1){
      app.msg('抱歉，您选择的图片超过最大选择范围');
   }
   }
}

   }else{is_select=''}
this.setData({is_select:is_select,select_num:num});
app.globalData.img=is_select;
if(max==1 && num>0){
   console.log('返回',is_select)
   this.back();
}
   },
   load_more() {
if(this.data.can_load!=1){return}
this.data.page++;
this.load()
   },
   del_select(e){
var that=this;
var is_select=this.data.is_select;
if(!is_select){app.err('请选择需要删除的图片');return;}
wx.showModal({
  title: '警告',content:'是否删除所选择的图片，删除将无法恢复，是否继续？',success(res){
     if(res.confirm){
      var id=''
for(var i in is_select){
id+=is_select[i].id+','
}
fun.get({ac:'img_del',id:id}).then(res=>{
var list=that.data.list;
for(var i in res.list){
  delete(list['item_'+res.list[i].id])

}
that.setData({list:list,is_select:'',select_num:0})
})
     }
  }
})
   },
   select_c(e){
var c=e.currentTarget.dataset.c;
this.setData({c:c});
this.data.page=1;this.data.can_load=1;
this.load()
   },
   onReachBottom() {

   },
   img_view(e){
var list=this.data.list;
var index=e.currentTarget.dataset.index;
this.setData({img_preview:list[index],img_preview_index:index})
   },
   clean_img_preview(e){
      this.setData({img_preview:''})
   },
   /**
    * 用户点击右上角分享
    */
   onShareAppMessage() {

   },go(e){ wx.navigateTo({url: e.currentTarget.dataset.url})},
   back(e){
      var pages=getCurrentPages();
      if(Object.keys(pages).length==1){this.home();}else{wx.navigateBack({delta:0})}
      },
   home(e){ wx.reLaunch({url: '/pages/index/index'})},
   rgo(e){wx.redirectTo({url: e.currentTarget.dataset.url})},
})